﻿package {
	import alternativa.engine3d.controllers.WalkController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Scene3D;
	import alternativa.engine3d.display.View;
	import alternativa.engine3d.materials.WireMaterial;
	import alternativa.engine3d.primitives.Box;
	import alternativa.types.Point3D;
	import alternativa.types.Texture;
	import alternativa.utils.FPS;
	import alternativa.utils.MathUtils;
	
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.utils.getTimer;

	public class TestColorTransform extends Sprite	{
		[Embed (source="test.jpg")]
		private static var bmpClass:Class;
		private var texture:Texture = new Texture(new bmpClass().bitmapData);
		private var material:TextureMaterialExt = new TextureMaterialExt(texture, 1, true, true);
		private var colorTransfrom:ColorTransform = new ColorTransform();
		
		private var scene:Scene3D;
		private var view:View;
		private var camera:Camera3D;
		private var container:Object3D;
		private var controller:WalkController;
		private var box:Box;
		
		public function TestColorTransform() {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.frameRate = 100;
			
			// Creating scene
			scene = new Scene3D();
			scene.root = new Object3D();
			box = new Box(100, 100, 100, 3, 3, 3);
			box.cloneMaterialToAllSurfaces(new WireMaterial(1, 0x7F7F7F));
			box.setMaterialToSurface(material, "front");
			scene.root.addChild(box);
			
			// Adding camera and view
			camera = new Camera3D();
			camera.y = -200;
			camera.rotationX = - MathUtils.DEG90;
			container = new Object3D();
			container.addChild(camera);
			scene.root.addChild(container);
			
			view = new View();
			addChild(view);
			view.camera = camera;

			// Connecting controller to the camera
			controller = new WalkController(stage);
			controller.object = container;
			controller.lookAt(new Point3D(-1, 2, -1));

			// FPS counter initialization
			FPS.init(stage);

			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			onResize(null);
		}
		
		private function onResize(e:Event):void {
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
		}
		
		private function onEnterFrame(e:Event):void {
			var time:uint = getTimer();
			// Texture's colors transformation
			colorTransfrom.redMultiplier = 0.2 + 0.4 * (1 + Math.cos(1e-3 * time));
			colorTransfrom.greenMultiplier = 0.2 + 0.4 * (1 + Math.cos(MathUtils.DEG45 + 3e-3 * time));
			colorTransfrom.blueMultiplier = 0.2 + 0.4 * (1 + Math.cos(MathUtils.DEG90 + 6e-3 * time));
			material.colorTransform = colorTransfrom;
			
			// Processing user input
			controller.processInput();
			// Scene calculation
			scene.calculate();
		}
	}
}